home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / Shape.java < prev    next >
Text File  |  1998-10-01  |  10KB  |  329 lines

  1. package symantec.itools.awt.shape;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Color;
  6. import java.awt.Graphics;
  7. import java.beans.PropertyVetoException;
  8. import java.beans.PropertyChangeListener;
  9. import java.beans.VetoableChangeListener;
  10. import symantec.itools.awt.BevelStyle;
  11.  
  12. //    06/02/97    LAB    Updated to support Java 1.1
  13. //  07/31/97    LAB    Made lightweight.  Added an override to setBackground to calculate bevel
  14. //                    colors.  Updated preferredSize to getPreferredSize.
  15. //  08/13/97    LAB    Added getMinimumSize which returns a 10 by 10 dimension.  Addresses Mac
  16. //                    Bug #7138.  Updated the color calculations to use new methods in ColorUtils.
  17. //    08/15/97    LAB    Reworked the way colors were calculated to avoid NullPointerExceptions,
  18. //                    and potential redraw problems.  Now colors are recalculated in paint,
  19. //                    if needed.
  20. //    09/16/97    RKM    Defaulted fillColor to Color.black
  21. //    10/01/98    EB  Replaced size by getSize; removed some wrong comments
  22.  
  23. /**
  24.  * Abstract class for shape components.
  25.  * This is the parent Shape class for the various
  26.  * shape components.
  27.  * @see symantec.itools.awt.shape.Ellipse
  28.  * @see symantec.itools.awt.shape.Circle
  29.  * @see symantec.itools.awt.shape.Rectangle
  30.  * @see symantec.itools.awt.shape.Square
  31.  * @see symantec.itools.awt.shape.VericalLine
  32.  * @see symantec.itools.awt.shape.HorizontalLine
  33.  * @version 1.1, June 2, 1997
  34.  * @author Symantec
  35.  */
  36. public abstract class Shape extends Component implements BevelStyle
  37. {
  38.     /**
  39.      * Construct default Shape with BEVEL_LINE style.
  40.      */
  41.     protected Shape()
  42.     {
  43.         style = BEVEL_LINE;
  44.         cachedBackground = getBackground();
  45.     }
  46.  
  47.     /**
  48.      * Sets the border style of the shape.
  49.      * @see #getBevelStyle
  50.      *
  51.      * @exception PropertyVetoException
  52.      * if the specified property value is unacceptable
  53.      */
  54.     public void setBevelStyle(int s) throws PropertyVetoException
  55.     {
  56.         if(style != s)
  57.         {
  58.             Integer oldValue = new Integer(style);
  59.             Integer newValue = new Integer(s);
  60.  
  61.             vetos.fireVetoableChange("BevelStyle", oldValue, newValue);
  62.  
  63.             style = s;
  64.             repaint();
  65.  
  66.             changes.firePropertyChange("BevelStyle", oldValue, newValue);
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * Returns the current style of the shape.
  72.      * @see #setBevelStyle
  73.      */
  74.     public int getBevelStyle()
  75.     {
  76.         return style;
  77.     }
  78.  
  79.     /**
  80.      * Sets the fill mode of the shape.
  81.      * @see #isFillMode
  82.      *
  83.      * @exception PropertyVetoException
  84.      * if the specified property value is unacceptable
  85.      */
  86.     public void setFillMode(boolean f) throws PropertyVetoException
  87.     {
  88.         if(fill != f)
  89.         {
  90.  
  91.             Boolean oldValue = new Boolean(fill);
  92.             Boolean newValue = new Boolean(f);
  93.  
  94.             vetos.fireVetoableChange("FillMode", oldValue, newValue);
  95.  
  96.             fill = f;
  97.             repaint();
  98.  
  99.             changes.firePropertyChange("FillMode", oldValue, newValue);
  100.         }
  101.     }
  102.  
  103.     /**
  104.      * Returns the current fill mode of the shape.
  105.      * @see #setFillMode
  106.      */
  107.     public boolean isFillMode()
  108.     {
  109.         return fill;
  110.     }
  111.  
  112.     /**
  113.      * @deprecated
  114.      * @see #isFillMode
  115.      */
  116.     public boolean getFillMode()
  117.     {
  118.         return isFillMode();
  119.     }
  120.  
  121.     /**
  122.      * Sets the fill color of the shape.
  123.      * @see #getFillColor
  124.      *
  125.      * @exception PropertyVetoException
  126.      * if the specified property value is unacceptable
  127.      */
  128.     public void setFillColor(Color color) throws PropertyVetoException
  129.     {
  130.         if(!symantec.itools.util.GeneralUtils.objectsEqual(fillColor, color))
  131.         {
  132.             Boolean oldValue = new Boolean(fill);
  133.  
  134.             vetos.fireVetoableChange("FillColor", oldValue, color);
  135.  
  136.             fillColor = color;
  137.             repaint();
  138.  
  139.             changes.firePropertyChange("FillColor", oldValue, color);
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Returns the current fill color of the shape.
  145.      * @see #setFillColor
  146.      */
  147.     public Color getFillColor()
  148.     {
  149.         return fillColor;
  150.     }
  151.  
  152.     /**
  153.      * Moves and/or resizes this component.
  154.      * This is a standard Java AWT method which gets called to move and/or
  155.      * resize this component. Components that are in containers with layout
  156.      * managers should not call this method, but rely on the layout manager
  157.      * instead.
  158.      *
  159.      * @param x horizontal position in the parent's coordinate space
  160.      * @param y vertical position in the parent's coordinate space
  161.      * @param width the new width
  162.      * @param height the new height
  163.      */
  164.     public void reshape(int x, int y, int width, int height)
  165.     {
  166.         this.width  = width;
  167.         this.height = height;
  168.  
  169.         super.reshape(x, y, width, height);
  170.     }
  171.  
  172.     /**
  173.      * Returns the minimum dimensions to properly display this component.
  174.      * This is a standard Java AWT method which gets called to determine
  175.      * the minimum size of this component.
  176.      * @see #getPreferredSize
  177.      */
  178.     public Dimension getMinimumSize()
  179.     {
  180.         return new Dimension(10, 10);
  181.     }
  182.  
  183.     /**
  184.      * Returns the recommended dimensions to properly display this component.
  185.      * This is a standard Java AWT method which gets called to determine
  186.      * the recommended size of this component.
  187.      * @see java.awt.Component#minimumSize
  188.      */
  189.     public Dimension getPreferredSize()
  190.     {
  191.         Dimension dim = getSize();
  192.         Dimension min = getMinimumSize();
  193.         return new Dimension(Math.max(dim.width, min.width), Math.max(dim.height, min.height));
  194.     }
  195.  
  196.     /**
  197.      * @deprecated
  198.      * @see getPreferredSize
  199.      */
  200.     public Dimension preferredSize()
  201.     {
  202.         return getPreferredSize();
  203.     }
  204.  
  205.     /**
  206.      * Paints this component using the given graphics context.
  207.      * This is a standard Java AWT method which typically gets called
  208.      * by the AWT to handle painting this component. It paints this component
  209.      * using the given graphics context. The graphics context clipping region
  210.      * is set to the bounding rectangle of this component and its [0,0]
  211.      * coordinate is this component's top-left corner.
  212.      *
  213.      * @param g the graphics context used for painting
  214.      * @see java.awt.Component#repaint
  215.      * @see java.awt.Component#update
  216.      */
  217.     public synchronized void paint(Graphics g)
  218.     {
  219.         //Make sure cached colors are correct.
  220.         Color curBackground = getBackground();
  221.         if (!symantec.itools.util.GeneralUtils.objectsEqual(curBackground, cachedBackground))
  222.         {
  223.             cachedBackground = curBackground;
  224.             updateBevelColors(curBackground);
  225.         }
  226.     }
  227.  
  228.     /**
  229.      * Adds a listener for all property change events.
  230.      * @param listener the listener to add
  231.      * @see #removePropertyChangeListener
  232.      */
  233.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  234.     {
  235.         changes.addPropertyChangeListener(listener);
  236.     }
  237.  
  238.     /**
  239.      * Removes a listener for all property change events.
  240.      * @param listener the listener to remove
  241.      * @see #addPropertyChangeListener
  242.      */
  243.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  244.     {
  245.         changes.removePropertyChangeListener(listener);
  246.     }
  247.  
  248.     /**
  249.      * Adds a listener for all vetoable property change events.
  250.      * @param listener the listener to add
  251.      * @see #removeVetoableChangeListener
  252.      */
  253.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  254.     {
  255.         vetos.addVetoableChangeListener(listener);
  256.     }
  257.  
  258.     /**
  259.      * Removes a listener for all vetoable property change events.
  260.      * @param listener the listener to remove
  261.      * @see #addVetoableChangeListener
  262.      */
  263.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  264.     {
  265.         vetos.removeVetoableChangeListener(listener);
  266.     }
  267.  
  268.     /**
  269.      * Used to calculate the colors for the different bevel styles.
  270.      * @param c The color of the background to make bevel colors from.
  271.      * @see java.awt.Component#addNotify
  272.      * @see java.awt.Component#setBackground
  273.      */
  274.     protected void updateBevelColors(Color c)
  275.     {
  276.         bevelDarkerColor    = symantec.itools.awt.util.ColorUtils.calculateShadowColor(c);
  277.         bevelLighterColor    = symantec.itools.awt.util.ColorUtils.calculateHilightColor(c);
  278.     }
  279.  
  280.     /**
  281.      * Width of this shape.
  282.      */
  283.     protected int width;
  284.  
  285.     /**
  286.      * Height of this shape.
  287.      */
  288.     protected int height;
  289.  
  290.     /**
  291.      * Border style of this shape.
  292.      */
  293.     protected int style;
  294.  
  295.     /**
  296.      * Shape is filled if true.
  297.      */
  298.     protected boolean fill;
  299.  
  300.     /**
  301.      * Color to fill shape with if fill is true.
  302.      */
  303.     protected Color fillColor = Color.black;
  304.  
  305.     /**
  306.      * The color to use as a hilight when BevelStyle is BEVEL_RAISED or BEVEL_LOWERED.
  307.      */
  308.     protected Color bevelLighterColor;
  309.     /**
  310.      * The color to use as a hilight when BevelStyle is BEVEL_RAISED or BEVEL_LOWERED.
  311.      */
  312.     protected Color bevelDarkerColor;
  313.     /**
  314.      * Cached value of the background color.  Used to determine if calculated colors need to be updated.
  315.      */
  316.     protected Color cachedBackground = null;
  317.  
  318.     /**
  319.      * Handles tracking vetoable change listeners and notifying them of each change
  320.      * to this component's properties.
  321.      */
  322.     protected symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  323.     /**
  324.      * Handles tracking non-vetoable change listeners and notifying them of each change
  325.      * to this component's properties.
  326.      */
  327.     protected symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  328. }
  329.